package main;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import utils.I18nTranslation;
import utils.SRTUtils;

/**
 * 
 *
 * @author xlouwette
 * @version $Revision:$ $Date:$ $Author:$
 */

public class SRTLanguage extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    
    private static final Map<Integer, Language> map = new HashMap<Integer, Language>();
    
    static{
        map.put(0,Language.ENGLISH);
        map.put(1,Language.DUTCH);
        map.put(2,Language.FRENCH);
    }
    
    public SRTLanguage(){
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,30,20));
        final JComboBox chooseLanguage = new JComboBox();
        JLabel language = new JLabel("Language : ");
        JButton confirm = new JButton("OK");
        JButton cancel = new JButton("Cancel");
        
        confirm.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                if(chooseLanguage.getSelectedIndex()!=-1){
                    Language language = (Language)chooseLanguage.getSelectedItem();
                    Properties properties = new Properties();
                    try {
                        properties.load(new FileInputStream(SRTUtils.CONFIGURATION));
                        properties.setProperty("locale",language.getLocale().toString());
                        properties.store(new FileOutputStream(SRTUtils.CONFIGURATION),"");
                        
                        SRTLanguage.this.dispose();
                    } catch (FileNotFoundException e1) {
                        throw new RuntimeException(e1);
                     } catch (IOException e1) {
                        throw new RuntimeException(e1);
                     }
                    
                }
            }
        });
        
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SRTLanguage.this.dispose();
            }  
        });
        
        
        
        chooseLanguage.addItem(Language.ENGLISH);
        chooseLanguage.addItem(Language.DUTCH);
        chooseLanguage.addItem(Language.FRENCH);
        panel.add(language);
        panel.add(chooseLanguage);
        panel.add(confirm);
        panel.add(cancel);
        
        add(panel);
        this.setPreferredSize(new Dimension(300,150));
        this.pack();
        this.setVisible(true);
    }
    
    enum Language{
        ENGLISH(I18nTranslation.ENGLISH,new Locale("EN")),
        DUTCH(I18nTranslation.DUTCH,new Locale("NL","BE")),
        FRENCH(I18nTranslation.FRENCH,new Locale("FR","BE"));
        
        private String translation;
        private Locale locale;
        
        private Language(String translation,Locale locale){
            this.translation = translation;
            this.locale = locale;
        }
        
        
        private Locale getLocale(){
            return this.locale;
        }
        
        @Override
        public String toString(){
            return this.translation;
        }
    }
    
}
